package com.sqlloader.views.listeners;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.sqlloader.db.ConnectionPool;

import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.sqlloader.views.DBAccessView;

public class TestDBAccessListener implements Listener {

	private DBAccessView parent;
	
	public TestDBAccessListener(DBAccessView parent)
	{
		this.parent=parent;
	}
	
	public void handleEvent(Event event) {
		// TODO Auto-generated method stub
       String username=parent.getUsernameText().getText();
       String password=parent.getPasswordText().getText();
       String dw=parent.getDwText().getText();
       String ip=parent.getIpText().getText();
       String url="jdbc:oracle:thin:@"+ip+":"+dw;
   	   ConnectionPool.getInstance().registerPool("oracle.jdbc.driver.OracleDriver", username, password, url, dw);
	   CCombo combo=parent.getTableCombo();
   	   combo.removeAll();
   	   
   	   
      try {
    	Connection c=ConnectionPool.getInstance().getConnection(dw);
		DatabaseMetaData metadata=c.getMetaData();
		ResultSet rs=metadata.getTables(null, username.toUpperCase(), null, new String[]{"TABLE"});
		while(rs.next())
		{
			String tablename=rs.getString("TABLE_NAME");
			combo.add(tablename);
		}
		
		c.close();
	   } catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	  } 
	}


}
